As we saw above, a function is a chunk of program which does something. You give the function a name so that you can refer to it in your program. Later on we will see that we can feed information into a function for it to work on, and functions can return results which we can use. For now we are going to consider just one function, which has the name main. The main function is special in that it is the one which is called to start your program, i.e. whatever other functions you have in your program, the main one is always called first. If you forget to put a main function in your program it won't start at all, and the compiler will complain at you.
To look at the header for main in more detail:
void main ( void )
The first void
means that the main function returns nothing of interest. By return I mean supply a value to the function which calls it. We will look at how this works a little later.
You will have noticed that I have coloured the word void
blue. This is because void
is a keyword. A keyword is a special word which means something in the C language. You can't create something and give it the name void
because C has reserved the word to mean "This is nothing of interest". I therefore can't make something of my own with the name "void".
The word main is in bold, because it is a function name which I have invented, we know it is special because we will use this name to call the function (and start the program).
The second void
is enclosed in brackets. The brackets are there because we may want to give a list of items here, and the brackets would mark the start and the end of our list. At the moment we don't want to put a list there, we simply want to tell the compiler that we don't want any parameters fed into the main function.
On the right you can see a CPIC loaded with two functions. The first one, fun, is not used at all, because when we run the program it starts at main. Later in the course we will see how we create and use functions of our own.
/* Calling the main function */
void fun ( void )
{
/* body of function fun */
/* which is not used in */
/* this program */
}
void main ( void )
{
/* body of the main function */
/* which is called when the */
/* program runs */
}